package Classes.GameUtils
{
	import Classes.GameDatas.GD_Building;
	import Classes.GameDatas.GD_BuildingItem;
	import Classes.GameDatas.GD_Building_Func;
	import Classes.GameDatas.GD_Item;
	import Classes.GameDatas.GD_Player;
	import Classes.GameDatas.GD_Weapon;
	import Classes.GameDatas.GameData;
	import Classes.GameErrors.GameError;
	import Classes.staticinfo.StaticInfo;
	
	import UIComponents.IUseItem;

	public class GD_deal
	{
		private static var _instance:GD_deal;
		public function GD_deal()
		{
		}
		
		public static function get instance():GD_deal
		{
			if(!_instance)_instance=new GD_deal();
			return _instance;
		}
		
		/**
		 *  输入ID  返回 前三位 即 ID类型  （默认）
		 * @param itemId
		 * @param index  返回前面几位  如果长度大于字符长度  则返回当前字符
		 * @return 
		 * 
		 */			
		public function getStr(itemId:Number,index:Number=3):Number
		{
			var tmpStr:String=String(itemId);
			if(tmpStr.length<index)
			{
				index=tmpStr.length;
			}
			var result:Number=Number(tmpStr.substr(0,index));
			return result;
		}
		
		/**
		 * 根据ID返回是什么类型  可以用来创建不同的GD类
		 * @param itemId  物品ID
		 * @return 
		 * 
		 */		
		public function reutrnType(itemId:Number):String
		{
			var type:Number=this.getStr(itemId);
			var kind:String="";
			switch(type)
			{
				case 101:
				case 102:
				case 103:
				case 130:
				case 104:
					kind=StaticInfo.ITEM_TYPE;
					break;
				case 110:
				case 111:
				case 112:
				case 113:
				case 114:	
					kind=StaticInfo.WEAPON_TYPE;
					break;
				case 401:
				case 402:
					kind=StaticInfo.BUILED_TYPE
					break;
				default:throw new GameError("物品ID跟类型无效,找不到此ID的信息!")
			}
			return kind;
		}
		
		
		/**
		 * 根据物品的ID跟类型获取具体信息 
		 * @param id
		 * @param type
		 * @return 
		 */		
		public function getDataByIdAndType(id:Number,type:Number):IUseItem
		{
			/**
			 *   物品 --类型为：101,102  ；收藏品 --类型为：130 
			 * 	  武器 --类型为：110,111,112,113,114
			 * 	  药品 --类型为：120,121,123,124   暂时不用
			 *   建筑---401；装饰--402
			 */
			var itemObj:IUseItem;
			switch(type)
			{
				case 101:
				case 102:
				case 103:
				case 104:
				case 130:
					itemObj=new GD_Item(id,0);
					break;
				case 110:
				case 111:
				case 112:
				case 113:
				case 114:	
					itemObj=new GD_Weapon(id,0);
					break;
				case 401:
				case 402:
					itemObj=new GD_BuildingItem(id);
					break;
				default:throw new GameError("物品ID跟类型无效,找不到此ID的信息!")
			}
			return itemObj;
		}
		
		/**
		 * 根据物品的ID来查找信息 
		 * @param id
		 * @return 
		 * 
		 */		
		public function getDataById(id:Number):IUseItem
		{
			var itemObj:IUseItem;
	
			if(null!=GD_Item.getItemConfigDataByType(id))
			{
				itemObj=new GD_Item(id,0);
			}else if(null!=GD_Weapon.itemConfigDatasObj[id])
			{
				itemObj=new GD_Weapon(id,0);
			}else if(null!=GD_BuildingItem.buildingItemsConfigObj[id])
			{
				itemObj=new GD_BuildingItem(id);
			}else
			{
				throw new GameError("物品ID无效,找不到此ID的信息!"+id)
			}
			return itemObj;
		}
		
		
		
		/**
		 * 根据对象ID返回对象数量 
		 * @param id
		 * @return 
		 */		
		public function getNumByObjId(id:Number):Number
		{
			var tmpNum:Number=0;
			var itemKind:String=this.reutrnType(id);
			switch (itemKind)
			{
				case StaticInfo.ITEM_TYPE: //物品
					var item:GD_Item=GD_Player.instance.getItemByType(id);
					tmpNum=item==null?0:item.itemNumber;
					break;
				case StaticInfo.WEAPON_TYPE: //武器    
					//没有装备的所有武器
					var weapon:GD_Weapon=GD_Player.instance.getWeaponByType(id);
					var wn:Number=weapon==null?0:weapon.itemNumber;
					//已经装备好的武器
					var xuanqWeapons:GD_Weapon=GD_Player.instance.getCurrentWeaponByType(id);
					var wn2:Number=xuanqWeapons==null?0:xuanqWeapons.itemNumber;
					tmpNum=wn+wn2;
					break;
				case StaticInfo.BUILED_TYPE://建筑
					var numFlag:Number=this.getStr(id);
					if(numFlag==401)
					{
						//功能ID
						var zt_fun_id:Number=GD_BuildingItem.buildingItemsConfigObj[id].zt_func_id;
						var zt_current_Lv:int=GD_BuildingItem.buildingItemsConfigObj[id].zt_func_greade;
						
						if(GD_Player.instance.character.characterState==0 && GD_Player.instance.mapData)
						{
							for each (var gd2:GD_Building_Func in GD_Player.instance.mapData.buildingsObj)
							{
								if(gd2.buildingType==zt_fun_id && gd2.buildingLv>=zt_current_Lv)
								{
									tmpNum=1;
									break;
								}
							}
						}else if(GD_Player.instance.character.characterState==2)
						{
							for each (var mygd2:GD_Building_Func in GD_Player.instance.myBuildObj)
							{
								if(mygd2.buildingType==zt_fun_id && mygd2.buildingLv>=zt_current_Lv)
								{
									tmpNum=1;
									break;
								}
							}
						}
						
						
						//判断在建筑的仓库中   本省就拥有  或者拥有 比它高的
						var buildItem2:GD_BuildingItem=GD_Player.instance.storeBuildings[id] as GD_BuildingItem; 
						var storeBuildId:Number=GD_Player.instance.hasbuildInStroeByFunid(zt_fun_id);
						var stroeBlulidLv:int=0;
						if(storeBuildId!=0){
							var tmpstroeBuild:GD_BuildingItem=GD_Player.instance.storeBuildings[storeBuildId] as GD_BuildingItem; 
							stroeBlulidLv=tmpstroeBuild.buildingLv;
						}
						
						if(buildItem2 || stroeBlulidLv>=zt_current_Lv)
						{
							tmpNum=1;
							break;
						}
						
					}else{
						var buildItem:GD_BuildingItem=GD_Player.instance.storeBuildings[id] as GD_BuildingItem;
						var bn:Number=buildItem==null?0:buildItem.buildingItemNum;
						var bn2:Number=0;
						if(GD_Player.instance.character.characterState==0 && GD_Player.instance.mapData)
						{
							for each (var gd:GD_Building_Func in GD_Player.instance.mapData.buildingsObj)
							{
								if(gd.buildingType==id)
								{
									bn2++;
								}
							}
						}else if(GD_Player.instance.character.characterState==2)
						{
							for each (var mygd:GD_Building_Func in GD_Player.instance.myBuildObj)
							{
								if(mygd.buildingType==id)
								{
									bn2++;
								}
							}
						}
						
						tmpNum=bn+bn2;
					}
					break;
			}
			return tmpNum;
		}
		
		
		public function isOwnbuildInMap(zt_fun_id:Number):Boolean
		{
			var falg:Boolean=false;
			if(GD_Player.instance.character.characterState==0 && GD_Player.instance.mapData)
			{
				for each (var gd2:GD_Building_Func in GD_Player.instance.mapData.buildingsObj)
				{
					if(gd2.buildingType==zt_fun_id)
					{
						falg=true;
						break;
					}
				}
			}
			return falg;
		}
		
		/**
		 *根椐id返回所拥有的物品信息 
		 * @param id
		 * @return 
		 * 
		 */
		public function getHaveItemById(id:Number):IUseItem
		{
			var tmpNum:Number=0;
			var itemKind:String=this.reutrnType(id);
			switch (itemKind)
			{
				case StaticInfo.ITEM_TYPE: //物品
					var item:GD_Item=GD_Player.instance.getItemByType(id);
					return item;
					break;
				case StaticInfo.WEAPON_TYPE: //武器    
					//没有装备的所有武器
					var weapon:GD_Weapon=GD_Player.instance.getWeaponByType(id);
					//已经装备好的武器
					var xuanqWeapons:GD_Weapon=GD_Player.instance.getCurrentWeaponByType(id);
					
					if(weapon!=null)return weapon;
					if(xuanqWeapons!=null)return xuanqWeapons;
					break;
			}
			
			return null;
		}
	}
}